package com.nf.controller.manage;

import com.google.gson.Gson;
import com.nf.controller.BaseServlet;
import com.nf.dao.SeatInfoDao;
import com.nf.dao.impl.SeatInfoDaoImpl;
import com.nf.entity.SeatInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 编写servlet，实现添加一个影厅的座位
 */
@WebServlet("/api/SeatAddServlet.do")
public class SeatAddServlet extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int seatRow= Integer.parseInt(req.getParameter("seatRow"));
        int seatCol= Integer.parseInt(req.getParameter("seatCol"));
        int hallId= Integer.parseInt(req.getParameter("hallId"));
        int showId= Integer.parseInt(req.getParameter("showId"));

        SeatInfo seat=new SeatInfo();
        SeatInfoDao dao=new SeatInfoDaoImpl();

        int r=0;
        for(int i=1;i<=seatRow;i++){
            for(int j=1;j<=seatCol;j++){

                seat.setSeatRow(i);
                seat.setSeatCol(j);
                seat.setHallId(hallId);
                seat.setShowId(showId);

                dao.add(seat);
                r++;
            }

        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        if(r==100){
            out.print(new Gson().toJson(successJson("添加成功")));
        }else {
            out.print(new Gson().toJson(errorJson("添加失败")));
        }
        out.flush();
        out.close();
    }
}
